<template>
	<div>
		<Row type="flex">
			<Col span="12">
				<Button type="primary" @click="addData">添加用户</Button>
			    <Button @click="handleSelectAll(true)">设置全选</Button>
		        <Button @click="handleSelectAll(false)">取消全选</Button>
		        <Button @click="removeSelectAll">删除用户</Button>
	        </Col>
			<Col span="12" justify="end" type="flex">
			    <Input v-model="filter.name" style="width:200px">
			        <Button slot="append" icon="ios-search" @click="search"></Button>
			    </Input>
			</Col>			
       	</Row>
	    <Table border :columns="columns7" :data="list" 
	    	border ref="selection"
	    	@on-selection-change="removeSelect"></Table>
	    <Page :total="filter.total" show-elevator show-total size="small" show-sizer @on-page-size-change="onPageSizeChange" :page-size="2" @on-change="changePage"></Page>
	    <Modal
	        v-model="modal1"
	        title="普通的Modal对话框标题"
	        @on-ok="ok"
	        @on-cancel="cancel">
	        
	         <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
		        <FormItem label="姓名" prop="name" >
		            <Input v-model="formValidate.name" placeholder="请输入姓名"></Input>
		        </FormItem>
		        <FormItem label="密码" prop="password">
		            <Input v-model="formValidate.password" placeholder="请输入姓名"></Input>
		        </FormItem>
		        <FormItem label="邮箱" prop="mail">
		            <Input v-model="formValidate.mail" placeholder="请输入邮箱"></Input>
		        </FormItem>
		        <FormItem>
		            <Button type="primary" @click="handleSubmit('formValidate')">提交</Button>
		            <Button type="ghost" @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button>
		        </FormItem>
		    </Form>
	    </Modal>
	</div>
</template>
<script>
	import Base from '../../common/Base.js'
    export default {
    	mixins:[Base],
        data () {
            return {
            	module:"users",         	
                columns7: [
                	{
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: '姓名',
                        key: 'name',
                        render: (h, params) => {
                            return h('div', [
                                h('Icon', {
                                    props: {
                                        type: 'person'
                                    }
                                }),
                                h('strong', params.row.name)
                            ]);
                        }
                    },
                    {
                        title: '密码',
                        key: 'password'
                    },
                    {
                        title: '邮箱',
                        key: 'mail'
                    },
                    {
                        title: '操作',
                        key: 'action',
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.show(params.index)
                                        }
                                    }
                                }, '查看'),
                                h('Button', {
                                    props: {
                                        type: 'success',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.editData(params.row._id)
                                        }
                                    }
                                }, '编辑'),
                                h('Button', {
                                    props: {
                                        type: 'error',
                                        size: 'small'
                                    },
                                    on: {
                                        click: () => {
                                            this.remove(params.row._id,params.index)
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    }
                ],            
                 formValidate: {
                    name: '',
                    password:'',
                    mail: ''
                },
                ruleValidate: {
                    name: [
                        { required: true, message: '姓名不能为空', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '密码不能为空', trigger: 'blur' }
                    ],
                    mail: [
                        { required: true, message: '邮箱不能为空', trigger: 'blur' },
                        { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
                    ]        
                }
            }
        },
        methods: {
        	handleSubmit (name) {
        	 	if(this.formValidate._id){
	                this.$refs[name].validate((valid) => {
	                    if (valid) {
	                        this.$Message.success('提交成功!');
	                        this.$http({
	                        	method:'POST',
	                        	url:'http://localhost:3000/users/data/'+this.formValidate._id,
	                        	data:this.formValidate
	                        }).then(response=>{
//	                        	this.formValidate=response.data
//	                        	this.reload()
//	                        	console.log(response)
//								this.$http({
//					        		method:'POST',
//					        		url:'http://localhost:3000/users/list',
//					        		data:{
//					        			page:this.page
//					        		}
//					        	}).then((response) => {
//					        		this.total=response.data.total
//      							this.data6=response.data.rows
//								})
								this.getData()
	                        })
	                    } else {
	                        this.$Message.error('表单验证失败!');
	                    }
	                })
	            }else{
//	            	delete this.formValidate._id;
	            	this.$refs[name].validate((valid) => {
	                    if (valid) {
	                    	console.log(this.formValidate)                   
	                    		this.$http({
		                   			method:"POST",
		                   			url:"http://localhost:3000/users/data",
		                   			data:this.formValidate
		                   		}).then(res=>{
		                   			this.formValidate=res.data                 			
		                   		})
	                        //添加数据	                   		
	                   		this.$Message.success('提交成功!');
	                    } else {
	                        this.$Message.error('表单验证失败!')  ;
	                    }
	                })
	            }
            },
//          reload(){
//          	this.$http({
//		        		method:'POST',
//		        		url:'http://localhost:3000/users/list'
//		        	}).then((response)=>{
//		        		this.total=response.data.total;
//		        		this.data6=response.data.rows;		        		
//		        	})
//          },
            handleReset (name) {
                this.$refs[name].resetFields();
            },
        	ok () {
                this.$Message.info('点击了确定');
            },
            cancel () {
                this.$Message.info('点击了取消');
            },
        	addData(){
        		this.modal1 = true
        	},
//      	changePage(page){
//	 				this.$http({
//	 					method:"POST",
//	 					url:"http://localhost:3000/users/list",
//	 					data:{
//	 						page:page
//	 					}
//	 				}).then((response)=>{
//	//      		console.log(response.data.rows)
//	        		this.total=response.data.total
//	        		this.data6=response.data.rows
//	        	})
//      	},
            show (index) {
                this.$Modal.info({
                    title: '用户信息',
                    content: `姓名：${this.list[index].name}<br>密码：${this.list[index].password}<br>邮箱：${this.list[index].mail}`
                })
            },
            //编辑用户
//          editData (id) {
//          	this.$http({
// 					method:"GET",
// 					url:"http://localhost:3000/users/data/"+id,
// 				}).then((response)=>{
//					this.formValidate=response.data;
//	        	})
//              this.modal1=true;
//          },         
//          remove (id,index) {
////          	console.log(this.data6)
//          	this.$http({
//	        		method:'delete',
//	        		url:'http://localhost:3000/users/data/'+id,	        		
//	        	}).then((response)=>{
//	        		this.data6.splice(index, 1);
//	        	})             
//          }
        }
    }
</script>